package com.house365.core.helper;

import java.io.File;

import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.DialogInterface.OnCancelListener;
import android.content.DialogInterface.OnClickListener;
import android.os.Handler;
import android.text.InputFilter;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.house365.core.R;
import com.house365.core.util.AbstractCallBack;
import com.house365.core.util.intent.IntentHelper;
import com.house365.core.view.ProgressBarDialog;

public class ViewHelper {

	/***
	 * ������1
	 * @param mContext
	 * @param titleId
	 * @param messageId
	 * @param listener
	 */
	public static void showDialog(Context mContext, int titleId, int messageId,
			final OnClickListener listener) {
		AlertDialog.Builder tDialog = new AlertDialog.Builder(mContext);
		// tDialog.setIcon(icon);
		tDialog.setTitle(titleId);
		tDialog.setMessage(messageId);
		tDialog.setPositiveButton(R.string.dialog_button_ok, listener);
		tDialog.show();
	}
	/***
	 * ������2
	 * @param context
	 * @param titleId
	 * @param messageId
	 * @param listener
	 */
	public static void showDialog(Context context, CharSequence title,
			CharSequence message, final OnClickListener listener) {
		AlertDialog.Builder tDialog = new AlertDialog.Builder(context);
		// tDialog.setIcon(icon);
		tDialog.setTitle(title);
		tDialog.setMessage(message);
		tDialog.setPositiveButton(R.string.dialog_button_ok, listener);
		tDialog.show();
	}
	/***
	 * �����Ի���1
	 * @param context
	 * @param titleId
	 * @param messageId
	 * @param listener
	 */
	public static void showConfirmDialog(final Context context,
			CharSequence title, CharSequence message,
			final OnClickListener listener) {
		AlertDialog.Builder tDialog = new AlertDialog.Builder(context);
		// tDialog.setIcon(R.drawable.info);
		tDialog.setTitle(title);
		tDialog.setMessage(message);
		tDialog.setPositiveButton(R.string.dialog_button_ok, listener);
		tDialog.setNegativeButton(
				context.getResources().getString(R.string.dialog_button_cancel),
				new DialogInterface.OnClickListener() {
					public void onClick(DialogInterface dialog, int which) {
						dialog.dismiss();
					}
				});
		tDialog.show();
	}
	/***
	 * �����Ի���2
	 * @param context
	 * @param titleId
	 * @param messageId
	 * @param listener
	 */
	public static void showConfirmDialog(final Context context,
			CharSequence title, CharSequence message, int textPositiveId,
			int textNegativeId, final OnClickListener listener) {
		AlertDialog.Builder tDialog = new AlertDialog.Builder(context);
		tDialog.setTitle(title);
		tDialog.setMessage(message);
		tDialog.setPositiveButton(textPositiveId, listener);
		tDialog.setNegativeButton(textNegativeId,
				new DialogInterface.OnClickListener() {
					public void onClick(DialogInterface dialog, int which) {
						dialog.dismiss();
					}
				});
		tDialog.show();
	}

	/***
	 * ��Ƚ���
	 * 
	 * @param context
	 * @param title
	 * @param message
	 * @param indeterminate
	 * @param cancelable
	 * @param listener
	 * @return
	 */
	public static ProgressBarDialog showProgress(Context context,
			CharSequence title, CharSequence message, boolean indeterminate,
			boolean cancelable, boolean canceledOnTouchOutside, OnCancelListener listener) {
		ProgressBarDialog dialog = new ProgressBarDialog(context);
		dialog.setTitle(title);
		dialog.setMessage(message);
//		dialog.setIndeterminate(indeterminate);
		dialog.setCancelable(cancelable);
		dialog.setCanceledOnTouchOutside(canceledOnTouchOutside);
		//dialog.setDefaultButton(false);
		dialog.setOnCancelListener(listener);
		dialog.show();
		return dialog;
	}
	/***
	 * �����������ؿ�
	 * @param context
	 * @param title
	 * @param message
	 * @param cancelable
	 * @param listener
	 * @return
	 */
	public static ProgressBarDialog showProgressHorizontal(Context context,
			CharSequence title, CharSequence message,
			boolean cancelable, boolean canceledOnTouchOutside, OnCancelListener listener) {
		ProgressBarDialog dialog = new ProgressBarDialog(context);
		dialog.setTitle(title);
		dialog.setMessage(message);
		dialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
//		dialog.incrementProgressBy(1);
//		dialog.setIndeterminate(false);
		dialog.setCancelable(cancelable);
		dialog.setCanceledOnTouchOutside(canceledOnTouchOutside);
		dialog.setOnCancelListener(listener);
		dialog.show();
		return dialog;
	}
	
	/***
	 * �򿪰�װ����
	 * 
	 * @param context
	 * @param apkfile
	 */
	public void showOpenFileDialog(final Context context, CharSequence title,
			CharSequence message, final String apkfile) {
		AlertDialog.Builder tDialog = new AlertDialog.Builder(context);
		// tDialog.setIcon(R.drawable.info);
		tDialog.setTitle(title);
		tDialog.setMessage(message);
		tDialog.setPositiveButton(R.string.dialog_button_ok,
				new DialogInterface.OnClickListener() {
					public void onClick(DialogInterface dialog, int which) {
						// FileUtil.chmod("777", apkfile);
						IntentHelper.openFileByType(context, new File(apkfile));
					}
				});
		tDialog.setNegativeButton(
				context.getResources().getString(R.string.dialog_button_cancel),
				new DialogInterface.OnClickListener() {
					public void onClick(DialogInterface dialog, int which) {
						dialog.dismiss();
					}
				});
		tDialog.show();
	}
	
	
	public static void showAlertDialogWithCloseInTime(
			AlertDialog.Builder alertDialogBuilder, long millisecond) {
		final AlertDialog alertDialog = alertDialogBuilder.show();
		new Handler().postDelayed(new Runnable() {

			@Override
			public void run() {
				if (alertDialog != null) {
					alertDialog.dismiss();
				}
			}
		}, millisecond);
	}

	public static void showAlertDialogWithCloseInTime(
			AlertDialog.Builder alertDialogBuilder, long millisecond,
			final AbstractCallBack callback) {
		final AlertDialog alertDialog = alertDialogBuilder.show();
		new Handler().postDelayed(new Runnable() {

			@Override
			public void run() {
				if (alertDialog != null) {
					alertDialog.dismiss();
				}
				callback.doCallBack();
			}
		}, millisecond);
	}

	public static void setButtonPressed(Button button, boolean pressed) {
		if (pressed) {
			button.setPressed(true);
			button.setFocusableInTouchMode(true);
			button.requestFocus();
		} else {
			button.setFocusable(false);
			button.setPressed(false);
			button.setFocusableInTouchMode(false);
		}
	}
	public static void showToastView(Context context, String msg) {
		View toastRoot = LayoutInflater.from(context).inflate(R.layout.toast,
				null);
		TextView message = (TextView) toastRoot.findViewById(R.id.message);
		message.setText(msg);
		Toast toastStart = new Toast(context);
		toastStart.setGravity(Gravity.CENTER, 0, 10);
		toastStart.setDuration(Toast.LENGTH_LONG);
		toastStart.setView(toastRoot);
		toastStart.show();
	}

	public static void showToastView(Context context, int resId) {
		View toastRoot = LayoutInflater.from(context).inflate(R.layout.toast,
				null);
		TextView message = (TextView) toastRoot.findViewById(R.id.message);
		message.setText(resId);
		Toast toastStart = new Toast(context);
		toastStart.setGravity(Gravity.CENTER, 0, 10);
		toastStart.setDuration(Toast.LENGTH_LONG);
		toastStart.setView(toastRoot);
		toastStart.show();
	}
	
	public static void showToast(Context context, CharSequence text) {
		Toast.makeText(context, text, Toast.LENGTH_SHORT).show();
	}
	public static void showToast(Context context, int resId) {
		Toast.makeText(context, resId, Toast.LENGTH_SHORT).show();
	}

	/***
	 * ����������������
	 * 
	 * @param context
	 * @param titleId
	 * @param key
	 * @param content
	 * @param maxSize
	 * @param inputType
	 * @param positiveClick
	 * 
	 *            demo:inputTextBox("�ǳ�:",regUser.nickName,7,InputType.
	 *            TYPE_TEXT_VARIATION_NORMAL);
	 */
	public static void inputTextBox(final Context context, int titleId,
			final String key, String content, int maxSize, int inputType,
			final InputTextBoxOnClickListener positiveClick) {
		AlertDialog.Builder alert = new AlertDialog.Builder(context);
		alert.setTitle(titleId);
		// alert.setIcon(android.R.drawable.ic_dialog_info);
		final EditText input = new EditText(context);
		InputFilter[] filters = new InputFilter[1];
		filters[0] = new InputFilter.LengthFilter(maxSize);
		input.setFilters(filters);
		input.setInputType(inputType);
		input.setText(content);
		input.setLines(1);
		alert.setView(input);
		alert.setPositiveButton(R.string.dialog_button_ok,
				new OnClickListener() {
					@Override
					public void onClick(DialogInterface dialog, int which) {
						positiveClick.onClick(dialog, which, input);
						dialog.dismiss();
					}
				});
		alert.setNegativeButton(R.string.dialog_button_cancel,
				new DialogInterface.OnClickListener() {
					public void onClick(DialogInterface dialog, int whichButton) {
						dialog.dismiss();
					}
				});
		alert.show();

	}

	/***
	 * ����������������
	 * 
	 * @param context
	 * @param titleId
	 * @param key
	 * @param content
	 * @param maxSize
	 *            ���Ƴ���
	 * @param inputType
	 * @param line
	 *            ����
	 * @param positiveClick
	 */
	public static void inputTextBox(Context context, int titleId,
			final String key, String content, int maxSize, int inputType,
			int line, final InputTextBoxOnClickListener positiveClick) {
		AlertDialog.Builder alert = new AlertDialog.Builder(context);
		alert.setTitle(titleId);
		final EditText input = new EditText(context);
		InputFilter[] filters = new InputFilter[1];
		filters[0] = new InputFilter.LengthFilter(maxSize);
		input.setFilters(filters);
		input.setInputType(inputType);
		input.setText(content);
		input.setLines(line);
		input.setGravity(Gravity.TOP);
		alert.setView(input);
		alert.setPositiveButton(R.string.dialog_button_ok,
				new OnClickListener() {

					@Override
					public void onClick(DialogInterface dialog, int which) {
						// TODO Auto-generated method stub
						positiveClick.onClick(dialog, which, input);
					}
				});
		alert.setNegativeButton(R.string.dialog_button_cancel,
				new DialogInterface.OnClickListener() {
					public void onClick(DialogInterface dialog, int whichButton) {
						dialog.dismiss();
					}
				});
		alert.show();

	}
	
	public interface InputTextBoxOnClickListener {
		public void onClick(DialogInterface dialog, int which, EditText view);

	}

}
